package api;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.Blog;
import dao.BlogDao;
import dao.User;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 28779
 * Date: 2024-03-19
 * Time: 20:39
 */
@WebServlet("/blog")
public class BlogServlet extends HttpServlet {
    private ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //查询数据库，获取到数据之后，构造成要求的 json 格式并返回。
        BlogDao blogDao = new BlogDao();
        String blogId = req.getParameter("blogId");
        if(blogId == null) {
            List<Blog> blogs = blogDao.getBlogs();
            String respJson = objectMapper.writeValueAsString(blogs);
            resp.setContentType("application/json; charset=utf8");
            resp.getWriter().write(respJson);
        } else {
            //此时获取的博客详情，有 blogId 参数
            Blog blog = blogDao.getBlog(Integer.parseInt(blogId));
            if (blog == null) {
                //返回一个 id 为 0 的 blog 对象，前端再根据这里进行判定。
                blog = new Blog();
            }
            String respJson = objectMapper.writeValueAsString(blog);
            resp.setContentType("application/json; charset=utf8");
            resp.getWriter().write(respJson);
        }
    }

    //实现用户编写博客功能
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        HttpSession session = req.getSession(false);
        if( session == null ) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("您尚未登录，请先登录再编写博客");
            return;
        }
        User user = (User) session.getAttribute("user");
        if(user == null) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("您尚未登录，请先登录再编写博客");
            return;
        }

        req.setCharacterEncoding("utf8");

        String content = req.getParameter("content");
        String title = req.getParameter("title");
        if(content == null || title == null || "".equals(content) || "".equals(title)) {
            resp.setContentType("text/html;charset=utf8");
            resp.getWriter().write("您输入的内容或者标题为空");
            return;
        }
        Integer userId = user.getUserId();
        Blog blog = new Blog();
        blog.setUserId(userId);
        blog.setTitle(title);
        blog.setContent(content);
        //这个可以不用写，因为在博客插入端已经写好了
        //blog.setPostTime(new Timestamp(System.currentTimeMillis()));

        BlogDao blogDao = new BlogDao();
        blogDao.insert(blog);

        resp.sendRedirect("blog_list.html");
    }
}
